﻿using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System;

namespace wxkq
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new AppHandleErrorAttribute());
        }
    }

    //public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
    //{
    //    public override void OnActionExecuting(ActionExecutingContext filterContext)
    //    {
    //        if (filterContext.HttpContext.Request.Url.ToString().IndexOf("localhost") > -1)
    //        {
    //            filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
    //            filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type");
    //            filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
    //        }
    //        else {
    //            filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
    //            filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type");
    //            filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
    //        }


    //        base.OnActionExecuting(filterContext);
    //    }
    //}

    public class AppHandleErrorAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            //记日志
            //Exception ex = filterContext.Exception;
            // if (ex is WebException)
            // {
            //     Dos.Common.LogHelper.Error(ex.ToString());
            //     //filterContext.ExceptionHandled = true;
            //     //filterContext.Result = new RedirectResult("/Home/error");
            // }
            // else
            // {
            //     //filterContext.Result = new Dos.Common.Jr();
            // }


            var ex = filterContext.Exception;
            string msg = string.Format("系统异常:{0}", ex.Message+ " Source:" + ex.Source);
            Dos.Common.LogHelper.Error(msg + " StackTrace:" + ex.StackTrace, "error");

            //filterContext.ExceptionHandled = true;
            //var result = new JsonResult();
            //result.Data = new Dos.Common.Jr(false, msg); //json;
            //result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            //filterContext.Result = result;

            #region Sample Segment（样例）
            //if (filterContext.HttpContext.Request.IsAjaxRequest())
            //{
            msg = "系统网络异常,请稍后重试";
            var result = new JsonResult();
            result.Data = new Dos.Common.Jr(false, null, msg, 0); //json;
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            filterContext.Result = result;
            //}
            //else
            //{
            //    filterContext.Result = new RedirectResult("Error");
            //}

            filterContext.ExceptionHandled = true;
            #endregion
        }
    }

    [Serializable]
    public class WebException : ApplicationException
    {
        public WebException() { }

        public WebException(string message)
            : base(message)
        {

        }

        public WebException(string message, Exception innerException)
            : base(message, innerException)
        {

        }
        protected WebException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
    }
}